﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.Events;

namespace RenderHeads.Media.AVProVideo.Demos
{
	// Token: 0x02000013 RID: 19
	public class SimpleController : MonoBehaviour
	{
		// Token: 0x06000056 RID: 86 RVA: 0x0000427C File Offset: 0x0000247C
		private void Start()
		{
			this._mediaPlayer.Events.AddListener(new UnityAction<MediaPlayer, MediaPlayerEvent.EventType, ErrorCode>(this.OnMediaPlayerEvent));
		}

		// Token: 0x06000057 RID: 87 RVA: 0x0000429C File Offset: 0x0000249C
		public void OnMediaPlayerEvent(MediaPlayer mp, MediaPlayerEvent.EventType et, ErrorCode errorCode)
		{
			switch (et)
			{
			case MediaPlayerEvent.EventType.MetaDataReady:
				this.GatherProperties();
				break;
			}
			this.AddEvent(et);
		}

		// Token: 0x06000058 RID: 88 RVA: 0x000042F0 File Offset: 0x000024F0
		private void AddEvent(MediaPlayerEvent.EventType et)
		{
			GlobalData.Debug_Log("[SimpleController] Event: " + et.ToString());
			this._eventLog.Enqueue(et.ToString());
			if (this._eventLog.Count > 5)
			{
				this._eventLog.Dequeue();
				this._eventTimer = 1f;
			}
		}

		// Token: 0x06000059 RID: 89 RVA: 0x0000435C File Offset: 0x0000255C
		private void GatherProperties()
		{
			if (this._mediaPlayer != null && this._mediaPlayer.Info != null)
			{
				this._width = this._mediaPlayer.Info.GetVideoWidth();
				this._height = this._mediaPlayer.Info.GetVideoHeight();
				this._durationSeconds = this._mediaPlayer.Info.GetDurationMs() / 1000f;
			}
		}

		// Token: 0x0600005A RID: 90 RVA: 0x000043D4 File Offset: 0x000025D4
		private void Update()
		{
			if (!this._useFading && this._display != null && this._display._mediaPlayer != null && this._display._mediaPlayer.Control != null)
			{
				this._display._color = Color.white;
				this._display._mediaPlayer.Control.SetVolume(1f);
			}
			if (this._eventLog != null && this._eventLog.Count > 0)
			{
				this._eventTimer -= Time.deltaTime;
				if (this._eventTimer < 0f)
				{
					this._eventLog.Dequeue();
					this._eventTimer = 1f;
				}
			}
		}

		// Token: 0x0600005B RID: 91 RVA: 0x000044A8 File Offset: 0x000026A8
		private void LoadVideo(string filePath, bool url = false)
		{
			if (!url)
			{
				this._nextVideoLocation = MediaPlayer.FileLocation.RelativeToStreamingAssetsFolder;
			}
			else
			{
				this._nextVideoLocation = MediaPlayer.FileLocation.AbsolutePathOrURL;
			}
			this._nextVideoPath = filePath;
			if (!this._useFading)
			{
				if (!this._mediaPlayer.OpenVideoFromFile(this._nextVideoLocation, this._nextVideoPath, this._mediaPlayer.m_AutoStart))
				{
					GlobalData.Debug_LogError("Failed to open video!");
				}
			}
			else
			{
				base.StartCoroutine("LoadVideoWithFading");
			}
		}

		// Token: 0x0600005C RID: 92 RVA: 0x00004524 File Offset: 0x00002724
		private static bool VideoIsReady(MediaPlayer mp)
		{
			return mp != null && mp.TextureProducer != null && mp.TextureProducer.GetTextureFrameCount() <= 0;
		}

		// Token: 0x0600005D RID: 93 RVA: 0x00004554 File Offset: 0x00002754
		private static bool AudioIsReady(MediaPlayer mp)
		{
			return mp != null && mp.Control != null && mp.Control.CanPlay() && mp.Info.HasAudio() && !mp.Info.HasVideo();
		}

		// Token: 0x0600005E RID: 94 RVA: 0x000045AC File Offset: 0x000027AC
		private IEnumerator LoadVideoWithFading()
		{
			float fade = 0.25f;
			while (fade > 0f && Application.isPlaying)
			{
				fade -= Time.deltaTime;
				fade = Mathf.Clamp(fade, 0f, 0.25f);
				this._display._color = new Color(1f, 1f, 1f, fade / 0.25f);
				this._display._mediaPlayer.Control.SetVolume(fade / 0.25f);
				yield return null;
			}
			yield return new WaitForEndOfFrame();
			yield return new WaitForEndOfFrame();
			yield return new WaitForEndOfFrame();
			if (Application.isPlaying)
			{
				if (!this._mediaPlayer.OpenVideoFromFile(this._nextVideoLocation, this._nextVideoPath, this._mediaPlayer.m_AutoStart))
				{
					GlobalData.Debug_LogError("Failed to open video!");
				}
				else
				{
					while (Application.isPlaying && (SimpleController.VideoIsReady(this._mediaPlayer) || SimpleController.AudioIsReady(this._mediaPlayer)))
					{
						yield return null;
					}
					yield return new WaitForEndOfFrame();
					yield return new WaitForEndOfFrame();
					yield return new WaitForEndOfFrame();
				}
			}
			while (fade < 0.25f && Application.isPlaying)
			{
				fade += Time.deltaTime;
				fade = Mathf.Clamp(fade, 0f, 0.25f);
				this._display._color = new Color(1f, 1f, 1f, fade / 0.25f);
				this._display._mediaPlayer.Control.SetVolume(fade / 0.25f);
				yield return null;
			}
			yield break;
		}

		// Token: 0x0600005F RID: 95 RVA: 0x000045C8 File Offset: 0x000027C8
		private void OnGUI()
		{
			if (this._mediaPlayer == null)
			{
				return;
			}
			GUI.depth = -10;
			if (this._guiSkin != null)
			{
				GUI.skin = this._guiSkin;
			}
			GUI.matrix = Matrix4x4.TRS(Vector3.zero, Quaternion.identity, new Vector3((float)Screen.width / 960f, (float)Screen.height / 540f, 1f));
			GUILayout.BeginVertical("box", new GUILayoutOption[0]);
			if (this._mediaPlayer.Control != null)
			{
				GUILayout.Label("Loaded: " + this._mediaPlayer.m_VideoPath, new GUILayoutOption[0]);
				GUILayout.Label(string.Format("Size: {0}x{1} FPS: {3} Duration: {2}ms", new object[]
				{
					this._width,
					this._height,
					this._mediaPlayer.Info.GetDurationMs(),
					this._mediaPlayer.Info.GetVideoFrameRate().ToString("F2")
				}), new GUILayoutOption[0]);
				GUILayout.Label(string.Concat(new object[]
				{
					"Updates: ",
					this._mediaPlayer.TextureProducer.GetTextureFrameCount(),
					"    Rate: ",
					this._mediaPlayer.Info.GetVideoDisplayRate().ToString("F1")
				}), new GUILayoutOption[0]);
				GUILayout.BeginHorizontal(new GUILayoutOption[0]);
				this._useFading = GUILayout.Toggle(this._useFading, "Fade to Black During Loading", new GUILayoutOption[0]);
				this._mediaPlayer.m_AutoStart = GUILayout.Toggle(this._mediaPlayer.m_AutoStart, "Auto Play After Load", new GUILayoutOption[0]);
				bool loop = this._mediaPlayer.m_Loop;
				bool flag = GUILayout.Toggle(loop, "Loop", new GUILayoutOption[0]);
				if (flag != loop)
				{
					this._mediaPlayer.m_Loop = flag;
					this._mediaPlayer.Control.SetLooping(flag);
				}
				GUILayout.EndHorizontal();
				int num = (int)this._mediaPlayer.Control.GetCurrentTimeMs();
				int num2 = (int)GUILayout.HorizontalSlider((float)num, 0f, this._durationSeconds * 1000f, new GUILayoutOption[0]);
				Rect lastRect = GUILayoutUtility.GetLastRect();
				float x = GUI.skin.horizontalSliderThumb.CalcSize(GUIContent.none).x;
				Rect position = lastRect;
				GUI.color = Color.green;
				position.xMin += x;
				position.y = position.yMax - 4f;
				position.width -= x * 1f;
				position.width *= this._mediaPlayer.Control.GetBufferingProgress();
				position.height = 4f;
				GUI.DrawTexture(position, Texture2D.whiteTexture, ScaleMode.StretchToFill);
				GUI.color = Color.green;
				int bufferedTimeRangeCount = this._mediaPlayer.Control.GetBufferedTimeRangeCount();
				for (int i = 0; i < bufferedTimeRangeCount; i++)
				{
					float num3 = 0f;
					float num4 = 0f;
					if (this._mediaPlayer.Control.GetBufferedTimeRange(i, ref num3, ref num4))
					{
						position.xMin = x + lastRect.x + (lastRect.width - x * 1f) * (num3 / (this._durationSeconds * 1000f));
						position.xMax = x + lastRect.x + (lastRect.width - x * 1f) * (num4 / (this._durationSeconds * 1000f));
						GUI.DrawTexture(position, Texture2D.whiteTexture, ScaleMode.StretchToFill);
					}
				}
				GUI.color = Color.white;
				if (num2 != num)
				{
					this._mediaPlayer.Control.Seek((float)num2);
				}
				if (!this._mediaPlayer.Control.IsPlaying())
				{
					if (GUILayout.Button("Play", new GUILayoutOption[0]))
					{
						this._mediaPlayer.Control.Play();
					}
				}
				else if (GUILayout.Button("Pause", new GUILayoutOption[0]))
				{
					this._mediaPlayer.Control.Pause();
				}
				GUILayout.BeginHorizontal(new GUILayoutOption[0]);
				int audioTrackCount = this._mediaPlayer.Info.GetAudioTrackCount();
				int currentAudioTrack = this._mediaPlayer.Control.GetCurrentAudioTrack();
				for (int j = 0; j < audioTrackCount; j++)
				{
					if (j == currentAudioTrack)
					{
						GUI.color = Color.green;
					}
					if (GUILayout.Button("Audio Track #" + (j + 1), new GUILayoutOption[0]))
					{
						this._mediaPlayer.Control.SetAudioTrack(j);
					}
					GUI.color = Color.white;
				}
				GUILayout.EndHorizontal();
			}
			GUILayout.Label("Select a new file to play:", new GUILayoutOption[0]);
			int num5 = GUILayout.SelectionGrid(-1, this._filenames, 3, new GUILayoutOption[0]);
			if (num5 >= 0)
			{
				this.LoadVideo(Path.Combine(this._folder, this._filenames[num5]), false);
			}
			GUILayout.Space(8f);
			GUILayout.Label("Select a new stream to play:", new GUILayoutOption[0]);
			int num6 = GUILayout.SelectionGrid(-1, this._streams, 1, new GUILayoutOption[0]);
			if (num6 >= 0)
			{
				this.LoadVideo(this._streams[num6], true);
			}
			GUILayout.Space(8f);
			GUILayout.Label("Recent Events: ", new GUILayoutOption[0]);
			GUILayout.BeginVertical("box", new GUILayoutOption[0]);
			int num7 = 0;
			foreach (string text in this._eventLog)
			{
				GUI.color = Color.white;
				if (num7 == 0)
				{
					GUI.color = new Color(1f, 1f, 1f, this._eventTimer);
				}
				GUILayout.Label(text, new GUILayoutOption[0]);
				num7++;
			}
			GUILayout.EndVertical();
			GUI.color = Color.white;
			GUILayout.EndVertical();
		}

		// Token: 0x04000049 RID: 73
		public string _folder = "AVProVideoSamples/";

		// Token: 0x0400004A RID: 74
		public string[] _filenames = new string[]
		{
			"SampleSphere.mp4",
			"BigBuckBunny_360p30.mp3",
			"BigBuckBunny_720p30.mp4"
		};

		// Token: 0x0400004B RID: 75
		public string[] _streams;

		// Token: 0x0400004C RID: 76
		public MediaPlayer _mediaPlayer;

		// Token: 0x0400004D RID: 77
		public DisplayIMGUI _display;

		// Token: 0x0400004E RID: 78
		public GUISkin _guiSkin;

		// Token: 0x0400004F RID: 79
		private int _width;

		// Token: 0x04000050 RID: 80
		private int _height;

		// Token: 0x04000051 RID: 81
		private float _durationSeconds;

		// Token: 0x04000052 RID: 82
		public bool _useFading = true;

		// Token: 0x04000053 RID: 83
		private Queue<string> _eventLog = new Queue<string>(8);

		// Token: 0x04000054 RID: 84
		private float _eventTimer = 1f;

		// Token: 0x04000055 RID: 85
		private MediaPlayer.FileLocation _nextVideoLocation;

		// Token: 0x04000056 RID: 86
		private string _nextVideoPath;
	}
}
